from django.shortcuts import render
from django.http import HttpResponse
from .utils import authCode
from django.core.cache import cache
from django.shortcuts import redirect


# Create your views here.
def login(request):
    if request.method == 'GET':
        return render(request, 'myapp/login.html')
    elif request.method == 'POST':
        user_name = request.POST.get('user_name')
        pwd = request.POST.get('pwd')
        u_code = request.POST.get('u_code')
        code = cache.get('code')
        print(u_code)
        print(code)
        if u_code != code:
            return redirect('myapp:login')
        else:
            return HttpResponse('登陆成功！')


def show_code(request):
    authCode.create_image()
    image = cache.get('image')
    return HttpResponse(image, content_type='image/png')